package com.polaris.live.adapter

import android.view.View
import com.polaris.live.R
import com.polaris.live.common.adapter.BaseBindingAdapter
import com.polaris.live.common.adapter.VBViewHolder
import com.polaris.live.common.constant.PagConst.LIVE_EFFECT_PAG
import com.polaris.live.databinding.AdapterSingleTopBinding
import com.polaris.live.resp.back_resp.DisplayUserBean
import com.polaris.live.utils.CommonUtils
import com.polaris.live.utils.state.UserStateUtils
import com.polaris.live.widget.avatar.FrameImageUtils
import com.polaris.live.widget.pag.AddFollowView.Companion.LIVE_ADD_FOLLOWE

/**
 * MainMessageFollowerAdapter
 *
 * @author Created by 半仙 on 2023/9/4/004 12:37
 */
class MainMessageSingleAdapter : BaseBindingAdapter<AdapterSingleTopBinding, DisplayUserBean>() {

    init {
        addChildClickViewIds(R.id.iv_focus)
    }

    override fun convert(holder: VBViewHolder<AdapterSingleTopBinding>, item: DisplayUserBean) {
        holder.vb.apply {
            country.setCountry(item.base.country)
            nickName.text = item.base.nickname
            if (CommonUtils.isMan(item.base.sex)) {
                level.setRichsLevel(item.base.richs)
            } else {
                level.setCharmsLevel(item.base.charms)
            }
            if (!item.follow) {
                llBg.setBackgroundResource(R.drawable.r8_back_ffeef5)
                ivFocus.visibility = View.VISIBLE
                ivFocus.apply {
                    setPathType(LIVE_ADD_FOLLOWE)
                    setRepeatCount(1)
                }
            } else {
                llBg.setBackgroundResource(R.drawable.r8_back_f8f9fa)
                ivFocus.visibility = View.INVISIBLE
            }
            userAvatar.setImageUrl(item.base.avatar, item.base.userId)
            avFv.setFrameImage(FrameImageUtils.urlToFrameImageBean(item.base?.avatarFrame))
            if (UserStateUtils.resolveUserState(item.base) == UserStateUtils.USER_STATE_LIVE ||
                UserStateUtils.resolveUserState(item.base) == UserStateUtils.USER_STATE_MATCH
            ) {
                ivBg.visibility = View.VISIBLE
                ivBg.apply {
                    path = LIVE_EFFECT_PAG
                    setRepeatCount(-1)
                    play()
                }
            } else {
                ivBg.visibility = View.INVISIBLE
            }

            when (val state = UserStateUtils.resolveUserState(item.base)) {
                UserStateUtils.USER_STATE_ONLINE, UserStateUtils.USER_STATE_OFFLINE, UserStateUtils.USER_STATE_BUSY -> {
                    vState.visibility = View.VISIBLE
                    ivStateLive.visibility = View.GONE
                    when (state) {
                        UserStateUtils.USER_STATE_ONLINE -> {
                            vState.setBackgroundResource(R.drawable.ic_user_space)
                        }

                        UserStateUtils.USER_STATE_OFFLINE -> {
                            vState.setBackgroundResource(R.drawable.shape_session_state_offline)
                        }

                        UserStateUtils.USER_STATE_BUSY -> {
                            vState.setBackgroundResource(R.drawable.ic_user_space)
//                            vState.setBackgroundResource(R.drawable.shape_session_state_busy)
                        }

                        else -> {}
                    }
                }

                UserStateUtils.USER_STATE_LIVE, UserStateUtils.USER_STATE_MATCH -> {
                    vState.visibility = View.GONE
                    ivStateLive.visibility = View.VISIBLE
                    when (state) {
                        UserStateUtils.USER_STATE_LIVE -> {
                            vState.visibility = View.VISIBLE
                            ivStateLive.visibility = View.GONE
                            vState.setBackgroundResource(R.drawable.ic_user_space)
//                            ivStateLive.setImageResource(null)
                        }

                        UserStateUtils.USER_STATE_MATCH -> {
                            vState.visibility = View.VISIBLE
                            ivStateLive.visibility = View.GONE
                            vState.setBackgroundResource(R.drawable.ic_user_space)
//                            ivStateLive.setImageResource(R.drawable.icon_list_match)
                        }

                        else -> {}
                    }
                }

                else -> {
                    vState.visibility = View.GONE
                    ivStateLive.visibility = View.GONE
                }
            }

        }
    }
}